export function moneyFlie(val) {
    if(!val||!Number(val))return '0.00';
    console.log(val,'val@@@@');
    return '-' + Number(val).toFixed(2)
}
export function inventoryWarning(val,num) {
    if(Number(num)>Number(val))return val+'  库存不足，请及时补货!'
    return val
}
export function staffType(val) {
    let list = ['','轮钟','点钟','加班1轮钟','加班1点钟','加班2轮钟','加班2点钟'];
    try {
        val = list[val];
    } catch (error) {
        
    }
    return val
}
export function leaveType(val) {
    let list = ['','事假','病假','产假','婚嫁','陪产假','其他','年假','调休假'];
    try {
        val = list[val];
    } catch (error) {
        
    }
    return val
}
//订单状态\r\n1: 待付款\r\n2: 待发货\r\n3: 待收货\r\n4: 已完成\r\n5: 已取消\r\n6: 退款中\r\n7: 已退款\r\n8: 已评价\r\n9: 部分退款\r\n10: 待消费
 export function orderStatus(val) {
    let list = ['','待付款','待发货','待收货','已完成','已取消','退款中','已退款','已评价','部分退款','待消费','已作废'];
    try {
        val = list[val];
    } catch (error) {

    }
    return val
}
export function orderType(val) {
    let list = ['未退款','已退款','退款中','退款驳回'];
    try {
        val = list[val];
    } catch (error) {
        
    }
    return val
}

export function typeGender(val) {
    switch (Number(val)) {
        case 0:
            val = '未知';
            break;
        case 1:
            val = '男';
            break;
        case 2:
            val = '女';
            break;
    }
    return val
 }
/**
 * Created by PanJiaChen on 16/11/18.
 */
/**
 * Parse the time to string
 * @param {(Object|string|number)} val
 * @param {string} cFormat 
 */
export function typeTitle(val) {
    switch (Number(val)) {
        case 1:
            val = '详情';
            break;
        case 2:
            val = '新增';
            break;
        case 3:
            val = '修改';
            break;
    }
    return val
}

//人员调动状态
export function moveType(val) {
    switch (Number(val)) {
        case 1:
            val = '调动';
            break;
        case 2:
            val = '离职';
            break;
    }
    return val
}
export function typeMode(val) {
    switch (val) {
        case 0:
            val = '大众点评';
            break;
        case 1:
            val = '小程序';
            break;
        case 2:
            val = '电话';
            break;
        case 3:
            val = '我的管家';
            break;
        default:
            val = '到店';
            break;
    }
    return val
 }
//用户状态
export function userType(val) {
    switch (Number(val)) {
        case -1:
            val = '停用';
            break;
        case 0:
            val = '停用';
            break;
        case 1:
            val = '正常';
            break;
    }
    return val
}
/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat 
 * @returns {string | null}
 */
export function YMDparseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}


/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}
